﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Controls;
using DotSpatial.Data;

namespace HAGISV2
{

    /// <summary>
    /// 用于表示文件夹
    /// </summary>
    [Serializable]
    public class GroupLayer:Layer
    {

        public GroupLayer(string id):base(id)
        {
            this._isSelectable = true;

            this._childs = new List<Layer>();
        }


        //用于存放一级子元素
        List< Layer> _childs;

        /// <summary>
        /// 所有的子元素
        /// </summary>
        public List<Layer> Childs
        {
            get { return _childs; }
            //set { m_Childs = value; }
        }



        #region 对外功能函数


        public void AddChild(Layer layer)
        {
            int insertMapIndex = this._defaultManager.LayerSystem.LayerCollection.GetSpatialMapLayerLastIndex(this.Id);

            layer.MapLayerIndex = insertMapIndex;

            layer.Parent = this;
            _childs.Add(layer);

            if (_isLoaded)
            {
                layer.DefaultManager = this._defaultManager;

                layer.Render();
            }
            
        }



        public void AddChildRange(List<Layer> layers)
        {
            int insertMapIndex = this._defaultManager.LayerSystem.LayerCollection.GetSpatialMapLayerLastIndex(this.Id);

            foreach (Layer layer in layers)
            {
                layer.MapLayerIndex = insertMapIndex;
                insertMapIndex++;

                layer.Parent = this;
                _childs.Add(layer);

                if (_isLoaded)
                {
                    layer.DefaultManager = this._defaultManager;

                    layer.Render();

                }



            }

            //this._defaultManager.LayerSystem.AskLayerChanged();
        }

        /// <summary>
        /// 删除一个子孩子
        /// </summary>
        /// <param name="id"></param>
        public void RemoveChild(string id)
        {

            Layer sub = this.GetChild(id);
            if (sub!=null)
            {
                if (_isLoaded)
                {
                    sub.StopRender();
                }

                sub.Parent = null;
                _childs.Remove(sub);
                
            }
        }

        /// <summary>
        /// 清空所有子目录
        /// </summary>
        public void RemoveAllChild()
        {

            foreach (Layer layer in this._childs)
            {
                layer.StopRender();
                layer.Parent = null;
            }

            this._childs.Clear();

            //this.m_ParenLManager.LayerSys.AskLayerChanged();

        }


        public bool GetMapLayerLastIndex(string id,ref int index)
        {
            for (int i = 0; i < _childs.Count; i++)
            {
                if (_childs[i].Id == id)
                {
                    index += _childs[i].MapLayerCount;
                    return true;
                }
                if (_childs[i].GetType() == typeof(GroupLayer))
                {
                    if (((GroupLayer)_childs[i]).GetMapLayerLastIndex(id, ref index))
                        return true;
                }
                else
                {
                    index += _childs[i].MapLayerCount;
                }
            }

            return false;
        }

        public Layer GetChild(string id)
        {
            Layer layer;
            for (int i = 0; i < _childs.Count; i++)
            {
                if (_childs[i].Id == id) return _childs[i];
                if (_childs[i].GetType() == typeof(GroupLayer))
                    if ((layer = ((GroupLayer)_childs[i]).GetChild(id)) != null) return layer;
            }
            return null;
        }

      



     

        //public List<Layer> SortedChilds
        //{
        //    get
        //    {

        //        List<Layer> temp = new List<Layer>();
        //        //temp.AddRange(this._childs.Values);

        //        temp.Sort(SmallToBig);

        //        return temp;


        //    }
        //}


        ///// <summary>
        ///// 内部图层使用的排序算法
        ///// </summary>
        ///// <param name="worker1"></param>
        ///// <param name="worker2"></param>
        ///// <returns></returns>
        //private static int SmallToBig(Layer worker1, Layer worker2)
        //{
        //    throw new NotImplementedException();
        //    //return worker1.PositonIndex - worker2.PositonIndex;

        //}


        #endregion


        #region 重写的函数

        public override Extent GetLayerExtent()
        {
            if (!this.IsLoaded)
            {
                return null;
            }

            Extent extent = null, helpEx = null;

            foreach (Layer item in Childs)
            {
                helpEx = item.GetLayerExtent();
                if (helpEx != null)
                    if (extent == null)
                        extent = helpEx;
                    else
                        extent.ExpandToInclude(helpEx);
            }

            return extent;
        }


        public override int MapLayerCount
        {
            get
            {
                int count=0;

                foreach (Layer item in this._childs)
                {
                    if (item is GroupLayer)
                    {
                        count += (item as GroupLayer).MapLayerCount;
                    }
                    else
                        count += item.MapLayerCount;
                }

                return count;
            }
        }


        protected override void OnViewModeChanged()
        {
            foreach (Layer item in this._childs)
            {
                item.ViewMode = this._viewMode;
            }
        }

        public override void Render()
        {
            foreach (Layer item in this._childs)
            {
                if (!item.IsLoaded)
                    item.Render();
                
            }

            this._isLoaded = true;
            OnSetVisible();
        }

        public override void StopRender()
        {
            foreach (Layer item in this._childs)
            {
                item.StopRender();
                
            }

            this._isLoaded = false;
        }

        public override bool OpenLayer()
        {
               foreach (Layer item in _childs)
                {
                    item.DefaultManager = this._defaultManager;

                    item.OpenLayer();
                }
           


            return true;
        }

        public override bool SaveLayer()
        {
            foreach (Layer item in _childs)
            {
              

                item.SaveLayer();

            }


            return true;
        }

        protected override void OnSetGisManager()
        {
            foreach (Layer item in this._childs)
            {
                item.DefaultManager=this.DefaultManager;

            }
           
        }


        protected override void OnSetVisible()
        {
            foreach (Layer item in this._childs)
            {
                item.Visible = this._visiable;
                
            }
        }

        protected override void OnSetLayerOpacity()
        {
            foreach (Layer item in this._childs)
            {
                item.Opacity = this.Opacity;

            }
            
        }

        public override void ClearSelection()
        {

            foreach (Layer item in this._childs)
            {

                if (item.IsSelectable)
                {
                    item.ClearSelection();

                    
                }

              
            }

        }
        #endregion


    }
}
